package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.ApplicationVariable;
import com.example.demo.common.StringTools;
import com.example.demo.common.UserSessionTools;
import com.example.demo.entity.ArticleInfo;
import com.example.demo.entity.UserInfo;
import com.example.demo.entity.vo.ArticleInfoVO;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/add")
    public AjaxResult add(ArticleInfo articleInfo, HttpServletRequest request) {
        // 非空效验
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getContent()) ||
            !StringUtils.hasLength(articleInfo.getTitle())){
            return AjaxResult.fail(-1,"参数异常","");
        }
        // 组装数据 得到uid
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        // 持久化 将结果转给前端
        int result = articleService.add(articleInfo);
        return AjaxResult.success(result);
    }

    /***
     * 获取文章详细信息但是需要进行验证(判断当前权限)
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/getdetailbyid")
    public AjaxResult getDetailById(Integer id, HttpServletRequest request){
        if(id == null || id <= 0) return AjaxResult.fail(-1,"参数非法","");
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        return AjaxResult.success(articleService.getDetailByIdAndUid(id,userInfo.getId()));
    }

    @RequestMapping("/update")
    public AjaxResult update(ArticleInfo articleInfo, HttpServletRequest request){
        // 非空效验
        if(articleInfo == null || articleInfo.getId() <= 0 ||
            !StringUtils.hasLength(articleInfo.getTitle()) ||
            !StringUtils.hasLength(articleInfo.getContent()))
            return AjaxResult.fail(-1,"参数有误","");
        // 获取登录用户id 填充到articleinfo对象中 (修改要验证权限)
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        int result = articleService.update(articleInfo);
        return AjaxResult.success(result);
    }

    @RequestMapping("/getdetail")
    public AjaxResult getDetail(Integer id){
        if(id == null || id <= 0) return AjaxResult.fail(-1,"参数有误","");
        ArticleInfoVO articleInfoVO = articleService.getDetail(id);
        return AjaxResult.success(articleInfoVO);
    }

    @RequestMapping("/addrcount")
    public AjaxResult addRCount(Integer id){
        if(id == null || id <= 0) return AjaxResult.fail(-1,"参数有误","");
        int result = articleService.addRCount(id);
        return AjaxResult.success(result);
    }

    @RequestMapping("/mylist")
    public AjaxResult myList(HttpServletRequest request) {
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        List<ArticleInfo> list = articleService.getListByUid(userInfo.getId());
        // 将文字正文截取成文章摘要
        for(ArticleInfo item : list){
            // todo: 将content 里面的 markdown 关键字去除掉
            item.setContent(StringTools.subLength(item.getContent(),200));
        }
        return AjaxResult.success(list);
    }

    @RequestMapping("/del")
    public AjaxResult del(Integer id, HttpServletRequest request){
        if(id == null || id <= 0) return AjaxResult.fail(-1,"参数有误","");
        UserInfo loginUser = UserSessionTools.getLoginUser(request);
        int result = articleService.del(id,loginUser.getId());
        return AjaxResult.success(result);
    }

    @RequestMapping("/getlistbypage")
    public AjaxResult getListByPage(Integer pageSize,Integer pageIndex) {
        // 默认值处理
        if(pageSize == null || pageSize == 0) {
            pageSize = 2;
        }
        if(pageIndex == null || pageIndex < 1) {
            pageIndex = 1;
        }
        int offset = (pageIndex - 1) * pageSize;
        List<ArticleInfo> list = articleService.getListByPage(pageSize,offset);
        // 多线程并发循环
        list.stream().parallel().forEach(item -> {
            item.setContent(StringTools.subLength(item.getContent(),150));
        });
        return AjaxResult.success(list);
    }

    @RequestMapping("/getcount")
    public AjaxResult getCount() {
        return AjaxResult.success(articleService.getCount());
    }
}
